﻿using System;
using System.Data;
using System.Web.Services.Protocols;
using MedicWeb.MedicService;

namespace MedicWeb.forms.Editors.Directory.Simple {
    public partial class SimpleEdit : SessionPage {
        protected void Page_Load(object sender, EventArgs e) {
            string s = Request.QueryString["id"];
            int id = s == null ? 0 : int.Parse(s);
            string directory = Request.QueryString["directory"];

            Form.Action = ResolveUrl("~/forms/Editors/Directory/Simple/SimpleEdit.aspx?directory=" + directory);
            if (s != null) {
                Form.Action += "&id=" + id;
            }

            if (directory == null) {
                throw new DataException();
            }
            var ms = new MainService();

            if (!IsPostBack) {
                DirectorySimpleItem item;

                switch (directory) {
                    case "AcademicTitle": {
                            if (id != 0) {
                                item = ms.LoadAcademicTitle(GetSession(), id);
                            } else {
                                item = new AcademicTitle();
                            }
                            break;
                        }
                    case "Class": {
                            if (id != 0) {
                                item = ms.LoadClass(GetSession(), id);
                            } else {
                                item = new Class();
                            }
                            break;
                        }
                    case "ClassSpecialisation": {
                            if (id != 0) {
                                item = ms.LoadClassSpecialisation(GetSession(), id);
                            } else {
                                item = new ClassSpecialisation();
                            }
                            break;
                        }
                    case "Degree": {
                            if (id != 0) {
                                item = ms.LoadDegree(GetSession(), id);
                            } else {
                                item = new Degree();
                            }
                            break;
                        }
                    case "InstitutionType": {
                            if (id != 0) {
                                item = ms.LoadInstitutionType(GetSession(), id);
                            } else {
                                item = new InstitutionType();
                            }
                            break;
                        }
                    case "JobCategory": {
                            if (id != 0) {
                                item = ms.LoadJobCategory(GetSession(), id);
                            } else {
                                item = new JobCategory();
                            }
                            break;
                        }
                    case "Ownership": {
                            if (id != 0) {
                                item = ms.LoadOwnership(GetSession(), id);
                            } else {
                                item = new Ownership();
                            }
                            break;
                        }
                    case "Region": {
                            if (id != 0) {
                                item = ms.LoadRegion(GetSession(), id);
                            } else {
                                item = new Region();
                            }
                            break;
                        }
                    default: {
                            throw new DataException();
                        }
                }

                tfId.Text = "" + item.Id;
                tfTitle.Text = item.Title;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            try {
                var ms = new MainService();
                string directory = Request.QueryString["directory"];
                switch (directory) {
                    case "AcademicTitle": {
                            var item = new AcademicTitle() { Id = Convert.ToInt32(tfId.Text), Title = tfTitle.Text };
                            item = ms.SaveAcademicTitle(GetSession(), item);
                            break;
                        }
                    case "Class": {
                            var item = new Class() { Id = Convert.ToInt32(tfId.Text), Title = tfTitle.Text };
                            item = ms.SaveClass(GetSession(), item);
                            break;
                        }
                    case "ClassSpecialisation": {
                            var item = new ClassSpecialisation() { Id = Convert.ToInt32(tfId.Text), Title = tfTitle.Text };
                            item = ms.SaveClassSpecialisation(GetSession(), item);
                            break;
                        }
                    case "Degree": {
                            var item = new Degree() { Id = Convert.ToInt32(tfId.Text), Title = tfTitle.Text };
                            item = ms.SaveDegree(GetSession(), item);
                            break;
                        }
                    case "InstitutionType": {
                            var item = new InstitutionType() { Id = Convert.ToInt32(tfId.Text), Title = tfTitle.Text };
                            item = ms.SaveInstitutionType(GetSession(), item);
                            break;
                        }
                    case "JobCategory": {
                            var item = new JobCategory() { Id = Convert.ToInt32(tfId.Text), Title = tfTitle.Text };
                            item = ms.SaveJobCategory(GetSession(), item);
                            break;
                        }
                    case "Ownership": {
                            var item = new Ownership() { Id = Convert.ToInt32(tfId.Text), Title = tfTitle.Text };
                            item = ms.SaveOwnership(GetSession(), item);
                            break;
                        }
                    case "Region": {
                            var item = new Region() { Id = Convert.ToInt32(tfId.Text), Title = tfTitle.Text };
                            item = ms.SaveRegion(GetSession(), item);
                            break;
                        }
                    default: {
                            throw new DataException();
                        }
                }
                Session["success"] = "Изменения сохранены успешно";
            } catch (Exception ex) {

                if (ex.InnerException is SoapException
                    && ((SoapException)ex.InnerException).Detail.InnerText == "AuthorizationException") {
                    throw ex;
                } else {
                    Session["error"] = "Ошибка сохранения";
                }
            }
        }
    }
}